package com.jprocms.module.cms.framework.operatelog.core;

import cn.hutool.core.util.StrUtil;
import com.jprocms.module.cms.dal.dataobject.channel.ChannelDO;
import com.jprocms.module.cms.service.channel.ChannelService;
import com.mzt.logapi.service.IParseFunction;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 获取栏目名称的 {@link IParseFunction} 实现类
 *
 * @author JPROCMS
 */
@Slf4j
@Component
public class ChannelParseFunction implements IParseFunction {

    public static final String NAME = "getChannelNameById";

    @Resource
    private ChannelService channelService;

    @Override
    public String functionName() {
        return NAME;
    }

    @Override
    public String apply(Object value) {
        if (StrUtil.isEmptyIfStr(value)) {
            return "";
        }

        // 获取栏目信息
        final ChannelDO channel = channelService.getChannel(Long.parseLong(value.toString()));
        if (channel == null) {
            //查不到栏目但是需要记录行为
            if (value != null) {
                return value.toString();
            }
            log.warn("[apply][获取内容{{}}为空", value);
            return "";
        }
        return channel.getName();
    }

}
